package com.zmn.oms.zmn.business.impl.log;

import com.zmn.oms.model.entity.log.FacadeTrackLog;
import com.zmn.oms.zmn.business.interfaces.log.FacadeTrackLogBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/07/07 11:36
 */
@Service("facadeTrackLogBService")
public class FacadeTrackLogBServiceImpl implements FacadeTrackLogBService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(FacadeTrackLog facadeTrackLog) {
        mongoTemplate.save(facadeTrackLog);
    }

    @Override
    public List<FacadeTrackLog> listByFacadeId(int facadeId, Date startTime) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("facadeId").is(facadeId)
                .and("beginTime").gte(startTime);
        query.addCriteria(criteria).limit(500);
        query.with(Sort.by(Sort.Order.desc("beginTime")));
        return mongoTemplate.find(query, FacadeTrackLog.class);
    }

    /**
     * 查询订单的所有同步日志
     * @return
     */
    @Override
    public List<FacadeTrackLog> listByOrderId(Long orderId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("orderId").is(orderId);
        query.addCriteria(criteria).limit(500);
        query.with(Sort.by(Sort.Order.desc("beginTime")));
        return mongoTemplate.find(query, FacadeTrackLog.class);
    }
}
